package ag.script.project.standard.core;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import com.google.gson.Gson;

import java.awt.*;
import java.io.File;

public class ScriptProject {

    private final String projectPath;//项目路径
    private final String projectMkdir;//项目目录
    private final String projectName;//项目名称
    private final String iconPath;//图标路径
    private final String desc;//项目描述


    //需要创建的目标

    private final File file_project;
    private final File file_res;
    private final File file_exts;
    private final File file_imgs;
    private final File file_info;
    private final File file_xmls;
    private final File file_desc;
    private final File file_list;
    private final File file_icon;
    private final File file_pkg_json;
    private final File file_log;


    public ScriptProject(String projectPath, String iconPath, String desc) {
        this.projectPath = projectPath;
        this.projectMkdir = FileUtil.getParent(projectPath, 1);
        this.projectName = FileUtil.getName(projectPath);
        this.iconPath = iconPath;
        this.desc = desc;
        this.file_project = new File(projectMkdir, projectName);
        this.file_log = new File(projectMkdir, "ag-script-standard.log");
        this.file_res = new File(file_project, "aigame-res");
        this.file_exts = new File(file_res, "exts");
        this.file_imgs = new File(file_res, "imgs");
        this.file_info = new File(file_res, "info");
        this.file_xmls = new File(file_res, "xmls");
        this.file_desc = new File(file_info, "desc.txt");
        this.file_list = new File(file_info, "list.txt");
        this.file_icon = new File(file_info, "icon.png");
        this.file_pkg_json = new File(file_project, "project.json");
    }


    public void create(ProjectJson projectJson) {

        if (!FileUtil.exist(file_project)) {
            FileUtil.mkdir(file_project);
        }

        if (!FileUtil.exist(file_res)) {
            FileUtil.mkdir(file_res);
        }
        if (!FileUtil.exist(file_exts)) {
            FileUtil.mkdir(file_exts);
        }
        if (!FileUtil.exist(file_imgs)) {
            FileUtil.mkdir(file_imgs);
        }
        if (!FileUtil.exist(file_info)) {
            FileUtil.mkdir(file_info);
        }
        if (!FileUtil.exist(file_xmls)) {
            FileUtil.mkdir(file_xmls);
        }
        if (FileUtil.exist(file_log)) {
            FileUtil.del(file_log);
        }
        if (!FileUtil.exist(file_pkg_json)) {
            if (projectJson == null) {
                FileUtil.touch(file_pkg_json);
            } else {
                FileUtil.writeUtf8String(new Gson().toJson(projectJson), file_pkg_json);
            }
        }
        if (!FileUtil.exist(file_desc)) {
            FileUtil.touch(file_desc);
            FileUtil.writeUtf8String(desc, file_desc);
        }
        if (!FileUtil.exist(file_list)) {
            FileUtil.touch(file_list);
        }
        if (!FileUtil.exist(file_icon)) {
            if (FileUtil.exist(iconPath)) {
                //读取图片
                Image img_icon = ImgUtil.scale(ImgUtil.read(iconPath), 200, 200);
                ImgUtil.write(img_icon, file_icon);
            } else {
                throw new RuntimeException(" icon file not exist !");
            }
        }
    }


    public File getFile_log() {
        return file_log;
    }

    public String getProjectPath() {
        return projectPath;
    }

    public String getProjectMkdir() {
        return projectMkdir;
    }

    public String getProjectName() {
        return projectName;
    }

    public String getIconPath() {
        return iconPath;
    }

    public String getDesc() {
        return desc;
    }

    public File getFile_project() {
        return file_project;
    }

    public File getFile_res() {
        return file_res;
    }

    public File getFile_exts() {
        return file_exts;
    }

    public File getFile_imgs() {
        return file_imgs;
    }

    public File getFile_info() {
        return file_info;
    }

    public File getFile_xmls() {
        return file_xmls;
    }

    public File getFile_desc() {
        return file_desc;
    }

    public File getFile_list() {
        return file_list;
    }

    public File getFile_icon() {
        return file_icon;
    }

    public File getFile_pkg_json() {
        return file_pkg_json;
    }
}
